package com.mercury.ec.module.air.service.demandorder;

import com.mercury.ec.module.air.controller.admin.demandorder.vo.demandroutesegment.AirDemandRouteSegmentPageReqVO;
import com.mercury.ec.module.air.controller.admin.demandorder.vo.demandroutesegment.AirDemandRouteSegmentSaveReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import com.mercury.ec.module.air.dal.dataobject.demandorder.AirDemandRouteSegmentDO;
import com.mercury.ec.framework.common.pojo.PageResult;
import com.mercury.ec.framework.common.util.object.BeanUtils;

import com.mercury.ec.module.air.dal.mysql.demandorder.AirDemandRouteSegmentMapper;

import static com.mercury.ec.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.mercury.ec.module.air.enums.ErrorCodeConstants.*;

/**
 * 机票需求订单方案行程航段 Service 实现类
 *
 * @author hesx
 */
@Service
@Validated
public class AirDemandRouteSegmentServiceImpl implements AirDemandRouteSegmentService {

    @Resource
    private AirDemandRouteSegmentMapper demandRouteSegmentMapper;

    @Override
    public Long createDemandRouteSegment(AirDemandRouteSegmentSaveReqVO createReqVO) {
        // 插入
        AirDemandRouteSegmentDO demandRouteSegment = BeanUtils.toBean(createReqVO, AirDemandRouteSegmentDO.class);
        demandRouteSegmentMapper.insert(demandRouteSegment);
        // 返回
        return demandRouteSegment.getId();
    }

    @Override
    public void updateDemandRouteSegment(AirDemandRouteSegmentSaveReqVO updateReqVO) {
        // 校验存在
        validateDemandRouteSegmentExists(updateReqVO.getId());
        // 更新
        AirDemandRouteSegmentDO updateObj = BeanUtils.toBean(updateReqVO, AirDemandRouteSegmentDO.class);
        demandRouteSegmentMapper.updateById(updateObj);
    }

    @Override
    public void deleteDemandRouteSegment(Long id) {
        // 校验存在
        validateDemandRouteSegmentExists(id);
        // 删除
        demandRouteSegmentMapper.deleteById(id);
    }

    private void validateDemandRouteSegmentExists(Long id) {
        if (demandRouteSegmentMapper.selectById(id) == null) {
            throw exception(AIR_DEMAND_ROUTE_SEGMENT_NOT_EXISTS);
        }
    }

    @Override
    public AirDemandRouteSegmentDO getDemandRouteSegment(Long id) {
        return demandRouteSegmentMapper.selectById(id);
    }

    @Override
    public PageResult<AirDemandRouteSegmentDO> getDemandRouteSegmentPage(AirDemandRouteSegmentPageReqVO pageReqVO) {
        return demandRouteSegmentMapper.selectPage(pageReqVO);
    }

}
